package com.iwhalecloud.bss.kite.cucc.spec.impl.query;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.goods.IGoodsService;
import com.iwhalecloud.bss.kite.cucc.dataservice.cache.CuccOfferOriginalInfoCache;
import com.iwhalecloud.bss.kite.cucc.spec.impl.transfer.GoodsInfoTransfer;
import com.iwhalecloud.bss.kite.dataservice.api.IQueryOfferDetailService;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Offer;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.OfferDetail;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Product;
import com.iwhalecloud.bss.kite.dataservice.mapper.spec.OfferDetailMapper;
import com.ztesoft.bss.common.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

@Service
public class OfferDetailQueryService implements IQueryOfferDetailService {

	@Autowired
	private IGoodsService goodsService;

	@Resource
    private OfferDetailMapper offerDetailMapper;

    @Override
    public List<OfferDetail> listOfferDetails(String offerId) {
        Offer offer = OfferCache.get(offerId);
        //如果是可选包类型的,明细取base产品的
        if (!KiteObjectUtils.isEmpty(offer) && StringUtil.equals(offer.getOfferType(), KeyConsts.OFFER_TYPE_12)) {
            //return GoodsInfoTransfer.convertAdditionalOfferDetails(offerId);
            //可选包直接返回空，需要刷新商品缓存
            return Lists.newArrayList();
        }
        else {
            Map repMap = CuccOfferOriginalInfoCache.get(offerId);
            if (KiteMapUtils.isEmpty(repMap)) {
                repMap = goodsService.queryGoodsByOfferId(offerId);
            }
            List<OfferDetail> offerDetails = GoodsInfoTransfer.convertOfferDetails(repMap);
            offerDetails.addAll(offerDetailMapper.listOfferDetails(offerId));
            return offerDetails;
        }
    }

	/*@Override
	public List<String> listRoleIdByProdId(String prodId) {
		//联通返回空
		//List<String> roleIds = offerDetailMapper.listRoleIdByProdId(prodId);
		return new ArrayList<>();
	}*/

	@Override
	public List<OfferDetail> listGiftOfferDetails(String offerId) {
		//组合商品需要添加实现
		return Collections.emptyList();
	}

	@Override
	public List<OfferDetail> listFuncProdRstrs(String offerId, String offerObjRelId, String rstrType) {
	    return offerDetailMapper.listFuncProdRstr(offerId, offerObjRelId, rstrType);
	}

	@Override
	public List<Product> listResProdsByByDetailId(String offerObjRelId, String rstrType) {
		//联通没有营销资源
		return Collections.emptyList();
	}

	@Override
	public List<Product> listResProdsByOfferId(String offerId, String roleId, String objId, String rstrType) {
		//联通没有营销资源
		return Collections.emptyList();
	}

	@Override
	public List<Product> listChoosableResProds(String offerObjRelId) {
		return Collections.emptyList();
	}

	@Override
	public List<OfferDetail> listOfferObjRelsByRstrCfgMin(String objId) {
		return Collections.emptyList();
	}

	@Override
	public List<OfferDetail> listOfferObjRelsByRstrCfgRstrType(String offerId, String rstrType) {
		return Collections.emptyList();
	}

	@Override
	public OfferDetail getOfferDetail(String offerObjRelId) {
		return null;
	}

}
